package product;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ProductInput extends JInternalFrame {

	HashMap<String, CodeData> cod;
	//제품 정보 저장
	
	ArrayList<ProductData> pro;
	//중복을 허용하는 ArrayList를 사용
	
	int count;
	//추가되는 제품마다 제품 번호를 저장

	DecimalFormat df = new DecimalFormat("###,###,###.##");
	//금액에 천단위 기호 입력
	
	private JLabel lbl2;
	private JLabel lbl1;
	private JProgressBar progressBar;
	private JLabel lblNewLabel_3;
	private JProgressBar progressBar_1;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_5;
	private JTextField proCode;
	private JTextField proName;
	private JTextField proPrice;
	private JTextField proEa;
	private JTextField proTot;
	private JButton btnNewButton;
	private JButton button;
	private JLabel status;
	/**
	 * Create the frame.
	 */
	public ProductInput(HashMap<String, CodeData> c,
			ArrayList<ProductData> p,int cnt) {
		super("ProductInput", true, true, true, false);
		this.cod = c;
		this.pro = p;
		this.count = cnt;
		setBounds(100, 100, 260, 280);
		getContentPane().setLayout(null);
		getContentPane().add(getLbl2());
		getContentPane().add(getLbl1());
		getContentPane().add(getProgressBar());
		getContentPane().add(getLblNewLabel_3());
		getContentPane().add(getProgressBar_1());
		getContentPane().add(getLblNewLabel_4());
		getContentPane().add(getLblNewLabel_5());
		getContentPane().add(getProCode());
		getContentPane().add(getProName());
		getContentPane().add(getProPrice());
		getContentPane().add(getProEa());
		getContentPane().add(getProTot());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getStatus());

	}

	public String replace(String s) {
			//텍스트 필드의 천단위 기호와 문자"원"을 제거
		String temp = s.replace(",","");
		String temp2 = temp.replace("원","");
		return temp2;
	}
	
	public void compute(){//수량 텍스트 필드에 key가 눌러질때 실행
		try {
			//단가와 수량을 입력받아 총액을 구해서 입력
			int ea = Integer.parseInt(proEa.getText());
			int price = Integer.parseInt(replace(proPrice.getText()));
			int tot = ea * price;
			String str = df.format((double)tot);
			proTot.setText(str+"원");
		} catch (Exception e) {	}
	}
	
	public void search(){//조회 매서드
		String str = proCode.getText();//코드
		CodeData value = cod.get(str);//CodeData에서 제품코드에 맞는 데이터를 value로 가져옴
		if(value==null) {//해당 코드가없을때 실행
			JOptionPane.showMessageDialog(null, "검색에 실패하였습니다.");
			return;
		}
		proName.setText(value.getProName());//찾아진 정보를 출력
		String str2 = df.format((double)value.getProPrice());
		proPrice.setText(String.valueOf(str2)+"원");
	}
	
	public void addValue(){//추가 매서드
		try {
			String code = proCode.getText();
			String name = proName.getText();
			int price = Integer.parseInt(replace(proPrice.getText()));//(,)와(원)을 제거
			int ea = Integer.parseInt(proEa.getText());
			int tot = Integer.parseInt(replace(proTot.getText()));//(,)와(원)을 제거
			ProductData data = new ProductData(code, name, price,ea, tot, count);
			count++;//생산번호를 하나 증가시킴
			ProductMain.setCount(count);//count변수를 main클래스로 보내줌
			pro.add(data);//ArrayList에 저장
			
			FileOutputStream fw = new FileOutputStream("ProductList.dat");
			ObjectOutputStream bw = new ObjectOutputStream(fw);
			bw.writeObject(pro);//파일저장
			bw.close();
			fw.close();
			ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream("count.ser"));
			dos.writeObject(new Integer(count));
				//count변수를 파일로 저장시켜 프로그램을 껏다가 켯을때도 생산번호가 이어져 나갈수 있게함
			dos.close();
			System.out.println("저장성공");
			status.setText(code + "저장됨");
		} catch (NumberFormatException en) {
			JOptionPane
					.showMessageDialog(null, "형식에 맞지않는 입력형식입니다.");
		} catch (Exception e) {
			System.out.println("error");
		}
	}
	private JLabel getLbl2() {
		if (lbl2 == null) {
			lbl2 = new JLabel("제품코드");
			lbl2.setBounds(12, 10, 57, 15);
		}
		return lbl2;
	}
	private JLabel getLbl1() {
		if (lbl1 == null) {
			lbl1 = new JLabel("제품명");
			lbl1.setBounds(12, 59, 57, 15);
		}
		return lbl1;
	}
	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setBounds(12, 35, 206, 14);
		}
		return progressBar;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("단가");
			lblNewLabel_3.setBounds(12, 84, 57, 15);
		}
		return lblNewLabel_3;
	}
	private JProgressBar getProgressBar_1() {
		if (progressBar_1 == null) {
			progressBar_1 = new JProgressBar();
			progressBar_1.setBounds(12, 109, 206, 14);
		}
		return progressBar_1;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("수량");
			lblNewLabel_4.setBounds(12, 133, 57, 15);
		}
		return lblNewLabel_4;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("금액");
			lblNewLabel_5.setBounds(12, 158, 57, 15);
		}
		return lblNewLabel_5;
	}
	private JTextField getProCode() {
		if (proCode == null) {
			proCode = new JTextField();
			proCode.setBounds(81, 7, 116, 21);
			proCode.setColumns(10);
		}
		return proCode;
	}
	private JTextField getProName() {
		if (proName == null) {
			proName = new JTextField();
			proName.setEditable(false);
			proName.setColumns(10);
			proName.setBounds(81, 56, 116, 21);
		}
		return proName;
	}
	private JTextField getProPrice() {
		if (proPrice == null) {
			proPrice = new JTextField();
			proPrice.setHorizontalAlignment(SwingConstants.RIGHT);
			proPrice.setEditable(false);
			proPrice.setColumns(10);
			proPrice.setBounds(81, 81, 116, 21);
		}
		return proPrice;
	}
	private JTextField getProEa() {
		if (proEa == null) {
			proEa = new JTextField();
			proEa.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent arg0) {
					compute();//계산 메서드
				}
			});
			proEa.setColumns(10);
			proEa.setBounds(81, 130, 116, 21);
		}
		return proEa;
	}
	private JTextField getProTot() {
		if (proTot == null) {
			proTot = new JTextField();
			proTot.setHorizontalAlignment(SwingConstants.RIGHT);
			proTot.setEditable(false);
			proTot.setColumns(10);
			proTot.setBounds(81, 155, 116, 21);
		}
		return proTot;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					search();//조회매서드
				}
			});
			btnNewButton.setBounds(12, 183, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("추가");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					addValue();//추가 메서드
				}
			});
			button.setBounds(121, 183, 97, 23);
		}
		return button;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel(" ");
			status.setOpaque(true);
			status.setBackground(Color.CYAN);
			status.setBounds(12, 216, 206, 15);
		}
		return status;
	}
}
